<?php
require 'vendor/autoload.php';

$app = new think\App();
$app->initialize();

echo "=== 调试菜单服务 ===\n\n";

// 模拟登录
$app->session->set('user', [
    'id' => 10000,
    'username' => 'admin',
    'authorize' => '',
    'status' => 1,
    'password' => md5('admin')
]);

echo "1. 检查是否登录:\n";
$isLogin = \think\admin\service\AdminService::isLogin();
echo "  登录状态: " . ($isLogin ? '是' : '否') . "\n";

echo "\n2. 检查是否超级管理员:\n";
$isSuper = \think\admin\service\AdminService::isSuper();
echo "  超级管理员: " . ($isSuper ? '是' : '否') . "\n";

echo "\n3. 获取用户信息:\n";
$userInfo = \think\admin\service\AdminService::getUserInfo();
print_r($userInfo);

echo "\n4. 直接从数据库查询菜单:\n";
try {
    $menus = \think\admin\model\SystemMenu::mk()
        ->where(['status' => 1])
        ->order('sort desc, id asc')
        ->select()
        ->toArray();
    echo "  数据库中的菜单数量: " . count($menus) . "\n";
    
    // 统计一级菜单
    $topMenus = array_filter($menus, function($m) { return $m['pid'] == 0; });
    echo "  一级菜单数量: " . count($topMenus) . "\n";
    foreach ($topMenus as $menu) {
        echo "    - {$menu['title']} (ID:{$menu['id']}, sort:{$menu['sort']})\n";
    }
} catch (\Exception $e) {
    echo "  错误: " . $e->getMessage() . "\n";
}

echo "\n5. 调用 MenuService::getTree():\n";
try {
    $tree = \think\admin\service\MenuService::getTree();
    echo "  返回的菜单树数量: " . count($tree) . "\n";
    
    if (empty($tree)) {
        echo "  ✗ 菜单树为空！\n";
    } else {
        echo "  菜单树内容:\n";
        foreach ($tree as $menu) {
            echo "    - {$menu['title']}\n";
            if (!empty($menu['sub'])) {
                foreach ($menu['sub'] as $sub) {
                    echo "      - {$sub['title']}\n";
                }
            }
        }
    }
} catch (\Exception $e) {
    echo "  错误: " . $e->getMessage() . "\n";
    echo "  堆栈: " . $e->getTraceAsString() . "\n";
}

echo "\n6. 检查缓存:\n";
$cacheKeys = ['SystemAuthNode', 'SystemMenu', 'MenuService'];
foreach ($cacheKeys as $key) {
    $cached = $app->cache->get($key);
    if ($cached) {
        echo "  {$key}: 有缓存 (" . (is_array($cached) ? count($cached) . '项' : '非数组') . ")\n";
    } else {
        echo "  {$key}: 无缓存\n";
    }
}

